using System;
using System.Collections.Generic;
using System.Linq;
using System.Configuration;
using CommonLogging;
using System.Web.Security;
using TestWeb.Models;
using TestWeb.Helpers;

namespace TestWeb.Repositories
{	
	/// <summary>
	/// The class is responsible for accessing table LoaiTinTuc in database
	/// </summary>
	public class LoaiTinTucRepository
	{
		#region Fields
		
		/// <summary>
		/// Get the number of elements in table LoaiTinTuc
		/// </summary>
		public int? TotalElems { get; set; }

		/// <summary>
		/// Calculate total pages from total items and page size
		/// </summary>
		public int TotalPages
		{
			get
			{
				if (this.TotalElems.HasValue && this.TotalElems.Value > 0)
                    return (this.TotalElems.Value / WebConfiguration.Instance.NumLoaiTinTucPerPage) + ((this.TotalElems.Value % WebConfiguration.Instance.NumLoaiTinTucPerPage == 0) ? 0 : 1);
				return 0;
			}
		}

		/// <summary>
		/// The logger instance
		/// </summary>
		public readonly ILogger Logger = LoggerManager.GetLogger(typeof(LoaiTinTucRepository).Name);

		#endregion
		
		#region Methods
		
		/// <summary>
		/// Select all of elements in the table LoaiTinTuc
		/// </summary>
		/// <returns>The collection of elements from Model</returns>
		public List<LoaiTinTuc> SelectAll()
		{
			var lst = new List<LoaiTinTuc>();
			try
			{
				this.TotalElems = DataContext.Instance.LoaiTinTuc.Count();
				lst = DataContext.Instance.LoaiTinTuc.ToList();
			}
			catch(Exception ex)
			{
				if(ex.InnerException == null)
					Logger.ErrorFormat("SelectAll: {0}", ex.Message);
				else
					Logger.ErrorFormat("SelectAll: {0}", ex.InnerException.Message);
			}
			return lst;
		}

		/// <summary>
		/// Select range of elements in the table LoaiTinTuc
		/// </summary>
		/// <param name="Page">The page which user want to go to</param>
		/// <returns>The collection of elements from Model</returns>
		public List<LoaiTinTuc> Select(int Page)
		{
			var lst = new List<LoaiTinTuc>();
			try
			{
				this.TotalElems = DataContext.Instance.LoaiTinTuc.Count();
				lst = DataContext.Instance.LoaiTinTuc.OrderBy(n => n.ID).Skip((Page - 1) * WebConfiguration.Instance.NumLoaiTinTucPerPage).Take(WebConfiguration.Instance.NumLoaiTinTucPerPage).ToList();
			}
			catch(Exception ex)
			{
				if(ex.InnerException == null)
					Logger.ErrorFormat("Select: {0}", ex.Message);
				else
					Logger.ErrorFormat("Select: {0}", ex.InnerException.Message);
			}
			return lst;
		}

		/// <summary>
		/// Select range of elements belong to Page from 1 to CurrentPage in the table LoaiTinTuc
		/// </summary>
		/// <param name="CurrentPageLoaded">The page which user went to</param>
		/// <returns>The collection of elements from Model</returns>
		public List<LoaiTinTuc> SelectToPage(int CurrentPageLoaded)
		{
			var lst = new List<LoaiTinTuc>();
			try
			{
				this.TotalElems = DataContext.Instance.LoaiTinTuc.Count();
				lst = DataContext.Instance.LoaiTinTuc.OrderBy(n => n.ID).Take(CurrentPageLoaded * WebConfiguration.Instance.NumLoaiTinTucPerPage).ToList();
			}
			catch(Exception ex)
			{
				if(ex.InnerException == null)
					Logger.ErrorFormat("SelectToPage: {0}", ex.Message);
				else
					Logger.ErrorFormat("SelectToPage: {0}", ex.InnerException.Message);
			}
			return lst;
		}

		/// <summary>
		/// Select specified element of table LoaiTinTuc
		/// </summary>
		/// <param name="ID">The key for identifying an element</param>
		/// <returns>The specified element from Model</returns>
		public LoaiTinTuc SelectByID(int ID)
		{
			try
			{
				return DataContext.Instance.LoaiTinTuc.FirstOrDefault(n => n.ID == ID);
			}
			catch(Exception ex)
			{
				if(ex.InnerException == null)
					Logger.ErrorFormat("SelectByID: {0}", ex.Message);
				else
					Logger.ErrorFormat("SelectByID: {0}", ex.InnerException.Message);
			}
			return null;
		}

		/// <summary>
		/// Insert a new element to table LoaiTinTuc
		/// </summary>
		/// <param name="newElem">The element will be inserted</param>
		/// <param name="errTxt">The error text</param>
		/// <returns>True: successful - False: failed</returns>
		public bool Insert(LoaiTinTuc newElem, ref string errTxt)
		{
			errTxt = string.Empty;
			try
			{
				DataContext.Instance.LoaiTinTuc.AddObject(newElem);
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException == null)
					errTxt = string.Format("Insert: {0}", ex.Message);
				else
					errTxt = string.Format("Insert: {0}", ex.InnerException.Message);
				Logger.Error(errTxt);
			}
			return false;
		}

		/// <summary>
		/// Delete an element in table LoaiTinTuc
		/// </summary>
		/// <param name="ID">The key for identifying an element</param>
		/// <param name="errTxt">The error text</param>
		/// <returns>True: successful - False: failed</returns>
		public bool Delete(int ID, ref string errTxt)
		{
			errTxt = string.Empty;
            LoaiTinTuc delitem = null;
			try
			{
				delitem = DataContext.Instance.LoaiTinTuc.FirstOrDefault(n => n.ID == ID );
				if (delitem != null)
				{
					DataContext.Instance.LoaiTinTuc.DeleteObject(delitem);
					DataContext.Instance.SaveChanges();
					return true;
				}
				else
					errTxt = string.Format("There is no any items with ID = {0} in table LoaiTinTuc", ID.ToString());
			}
			catch(Exception ex)
			{
				if (ex.InnerException == null)
					errTxt = string.Format("Delete: {0}", ex.Message);
				else
					errTxt = string.Format("Delete: {0}", ex.InnerException.Message);

                DataContext.Instance.LoaiTinTuc.Detach(delitem);
				Logger.Error(errTxt);
			}
			return false;
		}

		/// <summary>
		/// Update an specified element in table LoaiTinTuc
		/// </summary>
		/// <param name="errTxt">The error text</param>
		/// <returns>True: successful - False: failed</returns>
		public bool Update(ref string errTxt)
		{
			errTxt = string.Empty;
			try
			{
				DataContext.Instance.SaveChanges();
				return true;
			}
			catch(Exception ex)
			{
				if (ex.InnerException == null)
					errTxt = string.Format("Update: {0}", ex.Message);
				else
					errTxt = string.Format("Update: {0}", ex.InnerException.Message);
				Logger.Error(errTxt);
			}
			return false;
		}

		#endregion
		
	}
}
